package com.lizx.persional.resource;


import com.baomidou.kisso.SSOConfig;
import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.annotation.Action;
import com.baomidou.kisso.annotation.Login;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.kisso.common.encrypt.MD5Salt;
import com.baomidou.kisso.security.token.SSOToken;
import com.lizx.persional.common.utils.HttpContextUtils;
import com.lizx.persional.common.utils.Result;
import com.lizx.persional.entity.Users;
import com.lizx.persional.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;

/**
 * <p>
 * api
 * </p>
 *
 * @author 李振兴
 * @since 2018-02-27
 */
@RestController
@RequestMapping(value = "/persional/account", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class AccountResource {

    @Autowired
    private UsersService usersService;

    @Login(action = Action.Skip)
    @Permission(action = Action.Skip)
    @PostMapping("/login")
    public Result Login(String loginName, String password, boolean rememberMe) {
        /**
         * <p>
         * 用户存在，签名合法，登录成功
         * <br>进入后台
         *
         * </p>
         */
        Users user = usersService.selectByLoginName(loginName);
        if (user != null && MD5Salt.md5SaltValid(loginName, user.getPassword(), password)) {
            SSOToken st = new SSOToken();
            st.setId(user.getId());
            st.setData(loginName);

            // 记住密码，设置 cookie 时长 1 周 = 604800 秒
            if (rememberMe) {
                SSOConfig.getInstance().setCookieMaxage(604800);
            }
            SSOHelper.setCookie(HttpContextUtils.getHttpServletRequest(), HttpContextUtils.getHttpServletResponse(), st, true);
            return Result.success(user);
        } else {
            return Result.fail("用户名或密码错误");
        }
    }

    /**
     * 注册
     */
    @Login(action = Action.Skip)
    @Permission(action = Action.Skip)
    @PostMapping("/register")
    public Result register(@RequestBody Users user) {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        HttpServletResponse response = HttpContextUtils.getHttpServletResponse();

        Users existUser = usersService.selectByLoginName(user.getLoginName());
        if (existUser == null) {
            user.setPassword(MD5Salt.md5SaltEncode(user.getLoginName(), user.getPassword()));
            usersService.insert(user);
        } else {

            return Result.fail("该用户名已存在");
        }
        SSOToken st = new SSOToken();
        st.setId(user.getId());
        st.setData(user.getLoginName());
        SSOHelper.setCookie(request, response, st, true);
        return Result.success();
    }

    /**
     * 退出
     */
    @Login(action = Action.Skip)
    @Permission(action = Action.Skip)
    @GetMapping("/logout")
    public Result logout() {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        HttpServletResponse response = HttpContextUtils.getHttpServletResponse();
        SSOHelper.clearLogin(request, response);
        return Result.success();
    }

    /**
     * 修改密码
     *
     * @param password
     * @param newPassword
     * @return
     */
    @PostMapping("/updatePassword")
    public Result updatePassword(String password, String newPassword) {
        Users user = usersService.selectById(SSOHelper.getSSOToken(HttpContextUtils.getHttpServletRequest()).getId());
        if (MD5Salt.md5SaltValid(user.getLoginName(), user.getPassword(), password)) {
            user.setPassword(MD5Salt.md5SaltEncode(user.getLoginName(), newPassword));
            SSOHelper.clearLogin(HttpContextUtils.getHttpServletRequest(), HttpContextUtils.getHttpServletResponse());
            return Result.success(usersService.updateById(user));
        } else {
            return Result.fail("原密码输入错误！");
        }
    }

}
